// manager/pages/clockrecord/index.js
import { request } from '../../../utils/request';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: '',   //  1:营销打卡   2:包保打卡
        typeLabel: '',
        recordList: [],
        count: 0,
        countList: [],
        organList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            type: options.type,
            typeLabel: options.typeLabel
        })
        wx.setNavigationBarTitle({
            title: options.typeLabel
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.getClockRecordList();
        this.getClockRecordCount();
        this.getOrganList();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    // 获取本周打卡记录列表
    getClockRecordList() {      
        request({
            url: "/punchIn/getPunchInList",
            method: "GET",
            data: { 
                checkingType: this.data.type 
            }
        }).then(res => {
            // console.log(res)
            if(res.code == 0){
                this.setData({
                    recordList: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
     
    // 获取本周打卡记录统计
    getClockRecordCount() {   
        let {type} = this.data;   
        request({
            url: "/punchIn/getWeekPunchInCount",
            method: "GET",
            data: { 
                checkingType: this.data.type 
            }
        }).then(res => {
            // console.log(res)
            if(res.code == 0){
                if(type == 1){
                    this.setData({
                        count: res.data
                    })
                }else{
                    this.setData({
                        countList: res.data
                    })
                }
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    // 获取机构列表
    getOrganList(e) {      
        request({
            url: "/punchIn/getFixedPunchPoint",
            method: "GET",
        }).then(res => {
            // console.log(res)
            if(res.code == 0){
                this.setData({
                    organList: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    submit(){
        // 先获取经纬度
        this.getLatLng();
    },
    toPages(data){
        let {type,typeLabel} = this.data;
        if(type == 2){
            wx.navigateTo({
                url: `/manager/pages/clockorgan/index?type=${type}&typeLabel=${typeLabel}&latitude=${data.latitude}&longitude=${data.longitude}&address=${data.address}`,
            })
        }else{
            wx.navigateTo({
                url: `/manager/pages/clock/index?type=${type}&typeLabel=${typeLabel}&latitude=${data.latitude}&longitude=${data.longitude}&address=${data.address}`,
            })
        }
    },
    // 获取经纬度
    getLatLng(){
        let that = this;
        wx.getSetting({
            success: (res) => {
                console.log(res)
                if (res.authSetting['scope.userLocation'] == false) {//非初始化进入该页面,且未授权
                    wx.showModal({
                        title: '是否授权当前位置',
                        content: '需要获取您的地理位置，请确认授权',
                        success: function (res) {
                            if (res.confirm) {
                                wx.openSetting({
                                    success: function (data) {
                                        // console.log(data);
                                        if (data.authSetting["scope.userLocation"] == true) {
                                            that.getLocation();
                                        }
                                    }
                                })
                            }
                        }
                    })
                } else {//初始化进入
                    // console.log("KKKKK") 
                    that.getLocation();
                }
            }
        })
    },
    getLocation(){
        const that = this;
        wx.getLocation({
            type: 'gcj02',
            altitude: true,
            isHighAccuracy: true,
            success (res) {
                that.getCoordinatePositionInfo(res.latitude,res.longitude)
            },
            complete: function(res) {
                if(res.errMsg == "getLocation:fail auth deny"){
                    // wx.showToast({
                    //     title: '获取定位失败',
                    //     icon: 'none',
                    //     duration: 2000
                    // });
                }
            }
        })
    },
    getCoordinatePositionInfo(latitude,longitude){
        const that = this;
        request({
            url: "/commonApi/getCoordinatePositionInfo",
            method: "GET",
            data: {
                coordinate: `${latitude},${longitude}`
            }
        }).then(res => {
            if(res.code == 0){
                that.toPages({
                    latitude,
                    longitude,
                    address: res.data.address
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    }
})